package com.webside.Advertisingstatistics.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webside.Advertisingstatistics.mapper.AdProvinceStatMapper;
import com.webside.Advertisingstatistics.model.AdProvinceStatEntity;
import com.webside.Advertisingstatistics.model.AdbrowerRecordsEntity;
import com.webside.Advertisingstatistics.service.AdProvinceStatService;
import com.webside.Advertisingstatistics.service.AdbrowerRecordsService;
import com.webside.base.baseservice.impl.AbstractService;
import com.webside.resource.model.ResourceEntity;

/***
 * lpz 广告投放区域统计
 * 
 * @author Administrator
 *
 */
@Service("AdProvinceStatService")
public class AdProvinceStatServiceImpl extends AbstractService<ResourceEntity, Long> implements AdProvinceStatService {
	@Autowired
	private AdbrowerRecordsService adbrowerRecordsService;
	@Autowired
	private AdProvinceStatMapper adProvinceStatMapper;

	// 这句必须要加上。不然会报空指针异常，因为在实际调用的时候不是BaseMapper调用，而是具体的mapper，这里为userMapper
	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(adProvinceStatMapper);
	}

	@Override
	public int addadProvinceStat() {
		// TODO Auto-generated method stub
		// 每日向数据库中存广告投放区域 统计记录时先删除表中原数据
		int index = 0;
		int mark = 0;
		index = adProvinceStatMapper.deleteAdProvinceStat();
		if (index != 0) {
			// 各个地区的点击量的 统计集合
			List<AdbrowerRecordsEntity> pDPvList = new ArrayList<AdbrowerRecordsEntity>();
			pDPvList = adbrowerRecordsService.provinceDistributionPvList();

			for (AdbrowerRecordsEntity are : pDPvList) {
				AdProvinceStatEntity adProvinceStat = new AdProvinceStatEntity();
				adProvinceStat.setAdId(are.getAdResourceId());
				adProvinceStat.setAdTitle(are.getAdResourceTitle());
				adProvinceStat.setProvince(are.getProvince());
				adProvinceStat.setClickCount(are.getCount());
				mark = adProvinceStatMapper.addadProvinceStat(adProvinceStat);

			}

		} else {

			return mark;
		}

		return mark;
	}

	// 根据广告标题分组查询该广告下面所有省份的点击量
	@Override
	public List<AdProvinceStatEntity> adProvinceStatCkList(String adTitle) {
		// TODO Auto-generated method stub
		List<AdProvinceStatEntity> adProvinceStatList = new ArrayList<AdProvinceStatEntity>();
		adProvinceStatList = adProvinceStatMapper.adProvinceStatCkList(adTitle);
		return adProvinceStatList;
	}

}
